<?php 






 function sqcurl($url, $method='get', $data = array(), $header = array())
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT,3);   //只需要设置一个秒的数量就可以  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);          
        //设置header信息
        if(!empty($header)) {
            $header['Content-Type'] = 'application/x-www-form-urlencoded';
            $headerArr = array();
            foreach( $header as $n => $v ) {
                $headerArr[] = $n .':' . $v;
            }
            curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr );  //header设置
        }

        //设置请求方法,默认为get
        if($method != 'get') {
            curl_setopt($ch, CURLOPT_POST, 1);
        }

        //设置请求内容
        if(!empty($data)) {
            $s = http_build_query($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $s);
        }
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }























function info_back($obj,$mess="请求成功",$state=1){
    $data['obj']=$obj;
    $data['message']=$mess;
    $data['state']=$state;
    return json_encode($data);
}
function between_days ($day1, $day2)
{
  $second1 = strtotime($day1);
  $second2 = strtotime($day2);
    
  if ($second1 < $second2) {
    $tmp = $second2;
    $second2 = $second1;
    $second1 = $tmp;
  }
  return ($second1 - $second2) / 86400;
}

function upFileHandler($src_img, $sub_path, $img_domain = null, $limit_size = null)
{
    $tmpFile = $src_img['tmp_name'];
// echo "<pre>";
// var_dump($src_img);die;
 #print_r($src_img);die;
    $tmpFileSize = $src_img['size'];
    $maxSize = isset($limit_size) ? $limit_size : 10 * pow(1024, 2);
    if($tmpFileSize > $maxSize)
    {
        echo json_encode(array('status' => 0, 'msg' => '附件过大，请上传10MB以内大小附件！'));
        exit;
    }

    
    switch($src_img['type'])
    {
        /*case 'image/gif':
            $imgExt = '.gif';
            break;
        case 'image/jpeg':
        case 'image/pjpeg'://IE
            $imgExt = '.jpg';
            break;
        case 'image/png':
        case 'image/x-png'://IE
            $imgExt = '.png';
            break;
        case 'application/vnd.ms-excel':
        case 'application/msexcel':
        case 'application/octet-stream':
            $imgExt = '.xls';
            break;*/
        case 'application/msword':
            $imgExt = '.doc';
            break;
        case 'application/pdf':
            $imgExt = '.pdf';
            break;
        case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
            $imgExt = '.docx';
            break;
        case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
            $imgExt = '.xlsx';
            break;
        case 'application/vnd.ms-excel':
            $imgExt = '.xls';
            break;
        /*case 'application/zip':
            $imgExt = '.zip';
            break;*/
        default:
            break;
    }
    if(!isset($imgExt))
    {
        echo json_encode(array('status' => 0, 'msg' => '暂不支持该附件格式！'));
        exit;
    }

    $file_name = explode('.', $src_img['name']);
    $file_name = $file_name[0];
    $file_name1 = iconv('utf-8', 'gb2312', $file_name);
    $last_name = '_'.date("YmdHis") . '_' . rand(10000, 99999);
    $file_name = $file_name.$last_name;
    $file_name1 = $file_name1.$last_name;
    $savePath = APP_PATH . 'Uploads/file' . $sub_path . '/' . date('Y-m');
    $saveFile = $savePath . '/' . $file_name1 . $imgExt;

    

    //确认保存路径，没有则创建
    if(!is_dir($savePath))
    {
        if(!@mkdir($savePath, 0700, true))
        {
            echo json_encode(array('status' => 0, 'msg' => '上传目录创建失败！'));
            exit;
        }
    }
    //移动文件
    if(move_uploaded_file($tmpFile, $saveFile) === false)
    {
        echo json_encode(array('status' => 0, 'msg' => '附件上传失败！'));
        exit;
    }
    if(!isset($img_domain))
    {
        $img_domain = C('IMG_DOMAIN');
    }
   // $imgUrl = str_replace(APP_PATH . 'Uploads', $img_domain . '/Uploads', $saveFile);
    $saveFile = $savePath . '/' . $file_name . $imgExt;
    $imgUrl = str_replace(APP_PATH . 'Uploads', '/Uploads', $saveFile);
    echo json_encode(array('status' => 1, 'img_url' => $imgUrl, 'file_name'=>$file_name. $imgExt));
    //exit;
    return $imgUrl;
}
function get_cate_name($type_id,$cate_id){
    $cate_name=db("category")->where(array('id'=>$cate_id,'type_id'=>$type_id))->value('name');
    return $cate_name;
}
function get_cate_list($type_id){
    $where=array('t.id'=>$type_id);
    $list=db("category_type")->alias('t')->join('category c','t.id=c.type_id')->field('c.id,c.name')->where($where)->select();
    return $list;
}
function get_customer_name($id){
    $where=array('id'=>$id);
    $name=db("customer")->where($where)->value('customer_name');
    return $name;
}
function get_space_name($type_id,$id){
    if($type_id==1){
        $name=db('station')->where('id',$id)->value("station_name");
    }else{
        $name=db('site')->where('id',$id)->value("site_name");
    }
    return $name;
}
function site_type($type_id,$id){
    $name=db('category')->where(array('type_id'=>$type_id,'id'=>$id))->value('name');
    return $name;
}
function get_park_type($id){
    
    $type=db('station')->where('id',$id)->value("incubator_type");
    if($type==1){
        $name='园区';
    }else{
        $name='空间';
    }
    return $name;
}
function get_role_name($user_id){
    
 $info=db('roleUser')->alias('u')->join('role r','u.role_id=r.id')->where('user_id',$user_id)->field('r.id,r.name')->find();
 return $info;

}
function get_user_name($user_id){
    
 $info=db('user')->where('id',$user_id)->value("user_login");
 return $info;

}
function get_area($id){
    $area=db('station')->where('id',$id)->value('covered_area');
    return $area;
}
function get_station_type($id){
    $type=db('station')->where('id',$id)->value('type');
    return $type;
}
//  $info=db('user')->alias('u')->join('incubator i','u.incubator_id=i.id')->where('id',$id)->field('i.incubator_name')->find();
function get_incubator_name($id){
    $name=db('incubator')->where('id',$id)->value('incubator_name');
    return $name;
}
function get_customer_id($name){
    $id=db('customer')->where('customer_name',$name)->value('id');
    return $id;
}

function push_wx_info($user_id,$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1,$appid='',$pagepath=''){
  //  $url = 'v2.crejoy.cn/user/send_msg/service_notice';
    if($user_id){
        $data['user_id'] = $user_id;
    }else{
        return '该参数不能为空';
    }
    if($first){
        $data['first'] = $first;
    }else{
        return '该参数不能为空';
    }
    if($keyword1){
        $data['keyword1'] = $keyword1;
    }else{
        return '该参数不能为空';
    }
    if($keyword2){
        $data['keyword2'] = $keyword2;
    }else{
        return '该参数不能为空';
    }
    if($keyword3){
        $data['keyword3'] = $keyword3;
    }else{
        return '该参数不能为空';
    }
    if($remark){
        $data['remark'] = $remark;
    }else{
        return '该参数不能为空';
    }
    if($url1){
        $data['url'] = $url1;
    }else{
        if ($appid){
          $data['appid'] = $appid;
          $data['pagepath'] = $pagepath;  
        }
        else{
            return '该参数不能为空';
        }
    }

    $info = sqcurl($url, 'post', $data, array());
  //  file_put_contents('/hl/log.txt',print_r($info,true),FILE_APPEND);
    return $info;
}